/**
 * Created by xc on 2016/05/12.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbTmplPageModel = function(obj){
    ItbBaseModel.call(this,obj);

};
ItbTmplPageModel.prototype = new ItbBaseModel();

// 取得表名
ItbTmplPageModel.prototype.getTableName = function(){
    return 'itb_tmpl_page';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbTmplPageModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'page_id='+this.page_id;
}

//------------------------------------------------------------------
/* 根据模版id(多个时逗号连接)获取pages信息,
 注意:需要初始化 tmpl_ids 作为文档id
 * 参数:callback:回调，参数
 *        error:错误,null表示无错误
 *        pageModels:页面数据数组,每个元素为:ItbTmplPageModel结构
 */
ItbTmplPageModel.prototype.getPagesInfoByTmplIds = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbTmplPageModel.getPagesInfoByTmplIds Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT * FROM itb_tmpl_page t WHERE t.del_flg = 0 AND t.tmpl_id IN ( ' +
            THIS.tmpl_ids +
            ' ) ORDER BY t.tmpl_id, t.page_index ';
        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

//------------------------------------------------------------------
/* 根据模版页id(多个时逗号连接)获取pages信息,
 注意:需要初始化 page_ids 作为页id
 * 参数:callback:回调
 */
ItbTmplPageModel.prototype.getPageInfoById = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbTmplPageModel.getPageInfoById Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT p.*, t.user_id '+
            'FROM itb_tmpl_page p, itb_template t '+
            'WHERE p.del_flg=0 AND t.del_flg = 0 AND p.tmpl_id = t.tmpl_id AND p.page_id IN ( '+
            THIS.page_ids +
            ' ) ORDER BY p.tmpl_id, p.page_index ';
        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

//------------------------------------------------------------------
/* 获取所有模版页信息(不包含样板)
 * 模版切换功能用
 *
 * return: [{tmpl_id:111, pages:[xxx]},
 *          {tmpl_id:112, pages:[yyy]},
 *          ...]
 */
ItbTmplPageModel.prototype.getAllTmplPagesInfoForSwitch = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbTmplPageModel.getTmplPagesInfo Err:'+err);
        }
        // Use the connection
        var sql = ' SELECT t.tmpl_id, t.tmpl_name, ' +
            ' p.page_id, p.base_page_id, p.page_index, p.replaces, p.rep_hash_tag, p.backup_flg, p.thumb_s, ' +
            ' p.create_time, p.update_time, p.update_by ' +
            ' FROM itb_template t, itb_tmpl_page p ' +
            ' WHERE t.master=0 AND t.publish=1 AND t.del_flg = 0 AND t.tmpl_id=p.tmpl_id AND p.del_flg=0 ' +
            //    ' and t.tmpl_id in (4066,4017)'+// test
            ' ORDER BY p.tmpl_id DESC, p.page_index ';

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            var retInfo=[];
            var tmpId=null;
            var tmpPages=new Array();
            for(var i in result) {
                if(i==0) {
                    tmpId = result[0].tmpl_id;
                }

                if(result[i].tmpl_id != tmpId) {// 模版id变更
                    retInfo.push({
                        tmpl_id: tmpId,
                        pages: tmpPages
                    });

                    tmpId = result[i].tmpl_id;
                    tmpPages = new Array();
                }

                tmpPages.push({
                    page_id: result[i].page_id,
                    page_index: result[i].page_index,
                    base_page_id: result[i].base_page_id,
                    replaces: result[i].replaces,
                    rep_hash_tag: result[i].rep_hash_tag,
                    backup_flg: result[i].backup_flg,
                    thumb_s: result[i].thumb_s,
                    create_time: result[i].create_time,
                    update_time: result[i].update_time,
                    update_by: result[i].update_by
                });

                if(i==result.length-1) {// 最后一条
                    retInfo.push({
                        tmpl_id: tmpId,
                        pages: tmpPages
                    });
                }
            }

            callback(error, retInfo);
        });
    });
}

// 导出
module.exports = ItbTmplPageModel;